package com.hxwm.hwdbtool.service;

import com.hxwm.hwdbtool.core.HwException;
import com.hxwm.hwdbtool.util.HwUtil;

import java.sql.ResultSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @program: hwdbtool
 * @ClassName MapperRuleService
 * @description:
 * @author: hxwm
 * @create: 2024-06-08 11:12
 * @Version 1.0
 **/
public class MapperRuleService {

    private boolean multiLineFlag = false;
    private String multiLineValues;
    public static String emptyValueParse(boolean emptyFlag,String value){
        String result = null;
        if(HwUtil.isNullOrEmpty(value)) {
            if (emptyFlag) {
                result = "'',";
            } else {
                result = "NULL,";
            }
        }else{
            result = "'" + value + "',";
        }
        return result;
    }

    /**
     * 映射关系解析
     * @param oldValue
     * @param enumMapper
     * @param emptyFlag
     * @return
     */
    public String mapperFieldParse (String oldValue,Map<Object, Object> enumMapper,boolean emptyFlag){
        String result = null;
        if(HwUtil.isNullOrEmpty(oldValue)){
            if(!HwUtil.isNullOrEmpty(enumMapper)){
                oldValue = (String) enumMapper.get("NULL");
                if(oldValue != null){
                    result = "'" + oldValue + "',";
                }else{
                    oldValue = (String) enumMapper.get("EMPTY");
                    if(oldValue != null){
                        result = "'" + oldValue + "',";
                    }
                }
            }
        }else{
            if(!HwUtil.isNullOrEmpty(enumMapper)){
                String enumMapperValue = (String) enumMapper.get(oldValue);
                if(enumMapperValue != null && enumMapperValue.contains("|")){
                    result = "'" + enumMapperValue + "',";
                    this.multiLineValues = enumMapperValue;
                    this.multiLineFlag = true;
                } else if (enumMapperValue != null) {
                    result = "'" + enumMapperValue  + "',";
                }
            }
        }
        result = emptyValueParse(emptyFlag,result);
        return result;
    }
    /**
     * 特殊域
     * @param specialCondition
     * @param emptyFlag
     * @return
     */
    public String specialConditionParse(String specialCondition,boolean emptyFlag){
        String result = null;
        if("NOW".equals(specialCondition)){
            result = "NOW(),";
        } else if("GLOBAL".equals(specialCondition)){
            result = "'" + HwUtil.generateGlobalFlow() + "',";
        } else if (!HwUtil.isNullOrEmpty(specialCondition)){
            result = "'" + specialCondition + "',";
        }
        result = emptyValueParse(emptyFlag,result);
        return result;
    }

    /**
     * 关联关系语法解析
     * @param expression
     * @param resultSet
     * @return
     */
    public String relRelationParse (String expression, ResultSet resultSet,boolean emptyFlag) throws HwException {
        String result = null;
        try{
            String[] parseStr = expression.split("\\s+");
            String keyWord = parseStr[0].toLowerCase();
            if(keyWord.startsWith("if")){
                //获取if判断的条件字段
                String conditionField = parseStr[1].substring(parseStr[1].lastIndexOf(".") + 1);
                // 语法解析 是否存在equals
                if("equals".equals(parseStr[2].toLowerCase())){
                    //获取equals的条件列表
                    String conditions = parseStr[3];
                    //解析条件 格式(条件值,满足条件后的取值)
                    Map<Object, Object> conditionMap = HwUtil.enumMapper(conditions);
                    String conditionFieldValue = resultSet.getString(conditionField);
                    //循环条件赋值
                    for(Map.Entry<Object,Object> entry:conditionMap.entrySet()){
                        String value = (String) entry.getValue();
                        String key = (String) entry.getKey();
                        //若满足后的值 是"TABLE."开头，则表示需要获取数据库对应的值，否则返回原值
                        if(key.equals(conditionFieldValue)) {
                            if (value.startsWith("TABLE.")) {
                                result = resultSet.getString(value.substring(value.lastIndexOf(".") + 1));
                            } else {
                                result = value;
                            }
                            break;
                        }
                    }
                }

            }else if(keyWord.startsWith("concatdatetime")){
                Map<Object, Object> concatMap = HwUtil.enumMapper(parseStr[1]);
                Set<Object> objects = concatMap.keySet();
                for (Object key : objects) {
                    String dateStr = resultSet.getString((String)key);
                    String timeStr = resultSet.getString((String)concatMap.get(key));
                    if(!HwUtil.isNullOrEmpty(dateStr) && !HwUtil.isNullOrEmpty(timeStr)){
                        result = dateStr.replace("-","") + timeStr.replace(":","") ;
                    }else{
                        result = "";
                    }
                }

            }else if(keyWord.startsWith("concat")){
                List<String> concatStr = HwUtil.getConcatStr(parseStr[1]);
                for (String s : concatStr) {
                    if(s.startsWith("TABLE.")){
                        result = result + resultSet.getString(s.substring(s.lastIndexOf(".")+1));
                    }else{
                        result = result + s;
                    }
                }
            }else if(keyWord.startsWith("replace")){

            }
        }catch (Exception  e){
            throw new HwException("关联关系解析异常!");
        }
        if(HwUtil.isNullOrEmpty(result)) {
            if (emptyFlag) {
                result = "'',";
            } else {
                result = "NULL,";
            }
        }else {
            result = "'" + result + "'," ;
        }
        return result;
    }

    public boolean isMultiLineFlag() {
        return multiLineFlag;
    }

    public void setMultiLineFlag(boolean multiLineFlag) {
        this.multiLineFlag = multiLineFlag;
    }

    public String getMultiLineValues() {
        return multiLineValues;
    }

    public void setMultiLineValues(String multiLineValues) {
        this.multiLineValues = multiLineValues;
    }
}